using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using StockApi.Interfaces;

namespace StockApi
{
    public class StockWebUtility
    {
        private const string BaseStockUrlYahoo = "http://ichart.yahoo.com/table.csv?s=";

        public TResult GetStockResultFor<TParameter, TResult>(TParameter parameter, TResult result)
            where TParameter : IStockParameter, new() where TResult : IStockResult, new()
        {
            var hasStartDate = false;
            var stockStringBuilder = new StringBuilder();
            stockStringBuilder.Append(parameter.StockTicker);
            if (parameter.FromDate != null)
            {
                stockStringBuilder.Append("&a=");
                stockStringBuilder.Append(parameter.FromDate.Value.Month);
                stockStringBuilder.Append("&b=");
                stockStringBuilder.Append(parameter.FromDate.Value.Day);
                stockStringBuilder.Append("&c=");
                stockStringBuilder.Append(parameter.FromDate.Value.Year);
                hasStartDate = true;
            }
            if (parameter.ToDate != null && hasStartDate)
            {
                stockStringBuilder.Append("&d=");
                stockStringBuilder.Append(parameter.ToDate.Value.Month);
                stockStringBuilder.Append("&e=");
                stockStringBuilder.Append(parameter.ToDate.Value.Day);
                stockStringBuilder.Append("&f=");
                stockStringBuilder.Append(parameter.ToDate.Value.Year);
            }
            stockStringBuilder.Append("&g=");
            stockStringBuilder.Append(parameter.Interval);
            stockStringBuilder.Append("//&ignore=.csv");
            return GetStockResultStreamFor<TResult>(stockStringBuilder.ToString());
        }

        public IEnumerable<TResult> GetStockResultFor<TParameter, TResult>(IEnumerable<TParameter> parameters)
            where TParameter : IStockParameter, new() where TResult : IStockResult, new()
        {
            return parameters.ToList().Select(m => GetStockResultFor(m, new TResult()));
        }

        public bool ContainsDuplicates<TParameter>(IEnumerable<TParameter> parameters)
            where TParameter : IStockResult
        {
            return parameters.Distinct().Count() == parameters.Count();
        }

        public bool HasActiveInternetConnection()
        {
            return NetworkInterface.GetIsNetworkAvailable();
        }

        private TResult GetStockResultStreamFor<TResult>(string input)
            where TResult : IStockResult, new()
        {
            var req = (HttpWebRequest) WebRequest.Create(BaseStockUrlYahoo + input);
            var resp = (HttpWebResponse) req.GetResponse();
            var sr = new StreamReader(resp.GetResponseStream());
            var results = sr.ReadToEnd();
            sr.Close();
            return new TResult {MetaData = results};
        }
    }
}